// import 'dart:html';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/pages/home.dart';

// 商品列表
class ShowGoosList extends StatefulWidget {
  final onPress;
  final List<Map<String, dynamic>> goods;
  final ScrollController controller;
  const ShowGoosList(
      {Key? key, required this.goods, this.onPress, required this.controller})
      : super(key: key);

  @override
  State<ShowGoosList> createState() => _ShowGoosListState();
}

class _ShowGoosListState extends State<ShowGoosList> {
  @override
  Widget build(BuildContext context) {
    return ListView(
      // 无高度限制
      shrinkWrap: true,
      controller: widget.controller,
      // 滑动方向
      scrollDirection: Axis.vertical,
      children: widget.goods.map((Map map) {
        return Container(
            width: double.infinity,
            height: 130,
            margin: const EdgeInsets.fromLTRB(10, 10, 10, 0),
            decoration: const BoxDecoration(
                border: Border(bottom: BorderSide(color: Colors.grey))),
            child: InkWell(
                onTap: () {
                  // print(map);
                  // 跳转至详情页
                  Navigator.of(context)
                      .pushNamed("detail", arguments: map)
                      .then((value) => print(value));
                },
                child: Row(
                  // 排序顺序
                  textDirection: TextDirection.ltr,
                  // 开始方向 若mainAxisSize为min则没有意义
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  // mainAxisSize: MainAxisSize.min,
                  // 纵轴默认居中
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Image.asset(
                        map["url"],
                        fit: BoxFit.cover,
                        // 图片不重复
                        repeat: ImageRepeat.noRepeat,
                      ),
                      flex: 1,
                    ),
                    const SizedBox(
                      width: 20,
                    ),
                    Expanded(
                      child: Flex(
                        direction: Axis.vertical,
                        // 分布方式
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            map["title"],
                            style: const TextStyle(
                              fontSize: 26,
                              fontWeight: FontWeight.bold,
                            ),
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          ),
                          Text(
                            "￥" + map["price"].toString(),
                            style: const TextStyle(
                              fontSize: 20,
                              color: Colors.red,
                            ),
                          )
                        ],
                      ),
                      flex: 2,
                    )
                  ],
                )));
      }).toList(),
    );
  }
}

class HomeModel {
  late List<SwiperList> swiperList;
  late List<Tabs> tabsList;

  HomeModel({
    required this.swiperList,
    required this.tabsList,
  });

  factory HomeModel.fromJson(Map<String, dynamic> json) {
    List<SwiperList> swiperList = List<SwiperList>.empty(growable: true);
    // print(json);
    json["bannerList"].forEach((item) {
      swiperList.add(SwiperList.fromJson(item));
    });

    List<Tabs> tabsList = List<Tabs>.empty(growable: true);
    json["tabList"].forEach((item) {
      tabsList.add(Tabs.fromJson(item));
    });
    return HomeModel(swiperList: swiperList, tabsList: tabsList);
  }
}

class SwiperList {
  String title = "";
  String url = "";

  SwiperList({
    required this.title,
    required this.url,
  });

  factory SwiperList.fromJson(Map<String, dynamic> json) {
    return SwiperList(title: json["title"], url: json["url"]);
  }
}

class Tabs {
  String title = "";
  String url = "";
  String path = "";

  Tabs({
    required this.title,
    required this.url,
    required this.path,
  });

  factory Tabs.fromJson(Map<String, dynamic> json) {
    return Tabs(title: json["title"], url: json["url"], path: json["path"]);
  }
}
